#抓取模块
import requests
from lxml import etree
from datetime import datetime, timedelta
import time
def spider(url1):
    url = url1
    header = {"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"

}
    res = requests.get(url, headers=header)
    root = etree.HTML(res.text)
    #工作名字列表
    job_list = []
    job_list = root.xpath('//*[@id="content_list_wrap"]/li/a/text()')

    #工作区域列表
    area_list = []
    for i in range(1,len(job_list)):
        area = root.xpath(f'//*[@id="content_list_wrap"]/li[{i}]/div[1]/span/text()')
        area_c = str(area[0]).replace("\xa0","")
        area_list.append(area_c)

    #浏览量列表
    num_list = []
    num_list = root.xpath('//*[@id="content_list_wrap"]/li/div[2]/span/@title')

    #发布时间列表
    time_list = []
    yesterday = datetime.today() - timedelta(days=1)
    yyesterday = datetime.today() - timedelta(days=2)
    for i in range(1,len(num_list)):
        appear = root.xpath(f'//*[@id="content_list_wrap"]/li[{i}]/div[3]/@title')
        appear_time = str(appear[0])
        if '分钟前' in appear_time or '小时前' in appear_time:
            time_list.append(datetime.now().strftime('%Y-%m-%d'))
        elif appear_time == '昨天':
            time_list.append(yesterday.strftime('%Y-%m-%d'))
        elif appear_time == '前天':
            time_list.append(yyesterday.strftime('%Y-%m-%d'))
        else:
            time_list.append(appear_time)
    return job_list,area_list,num_list,time_list
